package hero.mps.utils;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

public class ChineseExcelGenerator {

    private static final String[] GENDERS = {"男", "女"};
    private static final String[] LAST_NAMES = {
            "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨",
            "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜",
            "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎"
    };
    private static final String[] FIRST_NAMES = {
            "伟", "芳", "秀英", "娜", "敏", "静", "丽", "强", "磊", "军", "洋", "勇", "艳", "杰", "娟", "涛",
            "明", "超", "秀兰", "霞", "平", "刚", "桂英", "飞", "红", "玉兰", "兰", "冬梅", "雪", "丽娟",
            "刚", "庆", "芳", "荣", "平", "晓", "文", "林", "梅", "松", "栋", "强", "军", "美", "婷", "艳"
    };

    public static void main(String[] args) {
        String[] columns = {"姓名", "性别", "电话"};
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("学生信息");

        // Create a font for styling header cells
        Font headerFont = (Font) workbook.createFont();
        headerFont.setBold(true);
        headerFont.setFontHeightInPoints((short) 12);
        headerFont.setColor(IndexedColors.BLACK.getIndex());

        // Create a cell style with the font
        CellStyle headerCellStyle = workbook.createCellStyle();
        headerCellStyle.setFont(headerFont);

        // Create a row for headers
        Row headerRow = sheet.createRow(0);

        // Create cells for header row
        for (int i = 0; i < columns.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(columns[i]);
            cell.setCellStyle(headerCellStyle);
        }

        // Create other rows and cells with student data
        Random random = new Random();
        for (int i = 1; i <= 56; i++) {
            Row row = sheet.createRow(i);

            String name = LAST_NAMES[random.nextInt(LAST_NAMES.length)] + FIRST_NAMES[random.nextInt(FIRST_NAMES.length)];
            String gender = GENDERS[random.nextInt(GENDERS.length)];
            String phone = generateRandomPhoneNumber();

            row.createCell(0).setCellValue(name);
            row.createCell(1).setCellValue(gender);
            row.createCell(2).setCellValue(phone);
        }

        // Resize all columns to fit the content size
        for (int i = 0; i < columns.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // Write the output to a file
        try (FileOutputStream fileOut = new FileOutputStream("学生信息.xlsx")) {
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Closing the workbook
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Excel文件生成成功!");
    }

    // Generate a random phone number
    private static String generateRandomPhoneNumber() {
        Random random = new Random();
        StringBuilder phoneNumber = new StringBuilder("13");
        for (int i = 0; i < 9; i++) {
            phoneNumber.append(random.nextInt(10));
        }
        return phoneNumber.toString();
    }
}
